package com.softer.wxzj.common.operatorutil;

import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.shiro.ShiroUtil;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.SysCorpOperator;
import com.softer.wxzj.mapper.SysCorpOperatorMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 *    
 *  @ClassName:   
 *  @Description:  
 *  @Author:  zcz   
 *  @CreateDate: 2019-05-10     
 *  
 */
@Component
public class CorpOperatorUtil {
    @Autowired
    private SysCorpOperatorMapper sysCorpOperatorMapper;
    @Value("${pwd}")
    private String pwd;
    //1开发公司 2物业公司 3维修企业 4监理企业 5业主委员会 6居员会 8审计企业 9个人
    private String createOperNo(String type){
        String operNo = type + "00000";
        //查询最大账号
        String maxOperNo = sysCorpOperatorMapper.getMaxOperNoByOperType(type);
        if (StringUtils.isNotBlank(maxOperNo)) {
            operNo = String.valueOf(Integer.parseInt(maxOperNo) + 1);
        }
        return operNo;
    }
    /**
     * 创建用户
     * @param createFrom
     * @param type
     * @param CorpId
     * @param name
     * @return
     */
    public int saveSysCorpOperator(String createFrom,String type,String CorpId,String name){
        String operType = String.valueOf(Integer.parseInt(type));
        SysCorpOperator sysCorpOperator = new SysCorpOperator();
        sysCorpOperator.setCreateFrom(createFrom);
        sysCorpOperator.setOperNo(createOperNo(operType));
        String salt = ShiroUtil.createSalt();
        String passWord = ShiroUtil.createPassWord(pwd, salt);
        sysCorpOperator.setOperPwd(passWord);
        sysCorpOperator.setSalt(salt);
        sysCorpOperator.setRoleCode(getRoleCode(operType));
        sysCorpOperator.setCreater(ShiroUserUtil.getUserName());
        sysCorpOperator.setLevel(NumStrEnum.ONE.getNum());
        sysCorpOperator.setCorpId(CorpId);
        sysCorpOperator.setName(name);
        sysCorpOperator.setOperType(operType);
        return sysCorpOperatorMapper.insert(sysCorpOperator);
    }

    /**
     * 创建roleCode
     * @param type
     * @return
     */
    private String getRoleCode(String type){
        String roleCode = null;
        //1开发公司 2物业公司 3维修企业 4监理企业 5业主委员会 6居员会 8审计企业 9个人
        switch (type){
            case "1" : roleCode = "1001";break;
            case "2" : roleCode = "1002";break;
            case "3" : roleCode = "1003";break;
            case "4" : roleCode = "1004";break;
            case "5" : roleCode = "1005";break;
            case "6" : roleCode = "1006";break;
            case "8" : roleCode = "1007";break;
            case "9" : roleCode = "1008";break;
        }
        return roleCode;
    }
}
